/**
* Returns an XMLHttp instance to use for asynchronous
* downloading. This method will never throw an exception, but will
* return NULL if the browser does not support XmlHttp for any reason.
* @return {XMLHttpRequest|Null}
*/
function createXmlHttpRequest() {
 try {
   if (typeof ActiveXObject != 'undefined') {
     return new ActiveXObject('Microsoft.XMLHTTP');
   } else if (window["XMLHttpRequest"]) {
     return new XMLHttpRequest();
   }
 } catch (e) {
   changeStatus(e);
 }
 return null;
};

/**
* This functions wraps XMLHttpRequest open/send function.
* It lets you specify a URL and will call the callback if
* it gets a status code of 200.
* @param {String} url The URL to retrieve
* @param {Function} callback The function to call once retrieved.
*/
function downloadUrl(url, callback) {
 var status = -1;
 var request = createTextHttpRequest();
 if (!request) {
   return false;
 }

 request.onreadystatechange = function() {
   if (request.readyState == 4) {
     try {
       status = request.status;
     } catch (e) {
       // Usually indicates request timed out in FF.
     }
     if (status == 200) {
       callback(request.responseXML, request.status);
       request.onreadystatechange = function() {};
     }
   }
 }
 request.open('GET', url, true);
 try {
   request.send(null);
 } catch (e) {
   changeStatus(e);
 }
};

function createChat(){
 downloadUrl('create?chatName='+ document.getElementById('chatName').value+ '&memeberAllowed',createChatResponse);
}
function deleteChat(){
downloadUrl('delete?chatName='+ document.getElementById('chatName').value, deleteChatResponse);
}
function sendAndUpdateChat(){
downloadUrl('update?chatName='+ document.getElementById('chatName').value+ 'message', updateChatResponse);
downloadUrl('giveChat?chatName='+ document.getElementById('chatName').value, updateChatResponse);
console.log('clicked');
}
function updateChat(){
downloadUrl('giveChat?chatName='+ document.getElementById('chatName').value, updateChatResponse);
}
function createChatResponse(response) {
console.log('Create Chat Worked');
groupOverflow.innerHTML = groupOverflow.innerHTML+ response;
}
function deleteChatResponse(){
consle.log('Delete Chat Worked');
}
function updateChatResponse(response1, response2){
consle.log('updating the chat works');
consle.log ('WHAT');
chatArea.innerHTML  = chatArea.innerHTML + response1;

}